<html>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<body>
<h1>乘客监听测试页面-模拟乘客客户端</h1>
<button onclick="setMessageContent('鼠标点我了')">测试message展示</button>
<div id="message">展示服务端推送过来消息的地方：</br></div>
<button onclick="sourceClose()">关闭连接</button>
<script>

    identity = 1;
    userId = "10";
    source = null;
    if(window.EventSource){
        console.log("此浏览器支持SSE");
        source = new EventSource("http://localhost:9000/connect?userId="+userId+"&identity="+identity);

        source.addEventListener("message", function (e) {
            const content = e.data;
            console.log("消息内容："+content);
            setMessageContent(content);

        })

    }else{
        setMessageContent("此浏览器不支持");
    }
    function setMessageContent(content){
        document.getElementById("message").innerHTML += (content+'</br>');
    }

    function sourceClose(){

        console.log("close方法执行")
        source.close();
        const httpRequest = new XMLHttpRequest();
        httpRequest.open("get","http://localhost:9000/close?userId="+userId+"&identity="+identity);
        httpRequest.send();
    }
</script>
</body>
</html>
